package feign;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * ***** 项目难点
 *
 *      问题描述
 *          openFeign调用不会传token（之前token事前端塞得，这里也需要手动塞），导致在结算功能中com.atguigu.spzx.order.service.impl.OrderInfoServiceImpl#getTrade()
 *          调用com.atguigu.spzx.feign.cart.CartFeignClient#getAllChecked()会出现NPE
 *
 *          因为token没传过去，无法从threadLocal中获取用户信息
 *
 *      问题解决方法
 *          利用openFeign的拦截器进行处理，手动塞入token信息
 *
 */
public class UserTokenFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("token");
        requestTemplate.header("token" , token) ;
    }

}